<?php

namespace app\common\model\offer;

use app\common\model\BaseModel;


class GoodsConfigModel extends BaseModel
{
    protected $table = 'offer_goods_config';

    /**
     * 格式化数据
     * @param $list
     * @return array
     */
    public function _formatList($list)
    {
        foreach($list as $k=>$v){
            $goodsConfig = json_decode($v['goods_config'],true);
            $v['goods_config'] = $goodsConfig;
            $v['select'] = $goodsConfig['attr'];
            $v['input'] = $goodsConfig['input'];
            $v['goods_attr'] = implode(",",$goodsConfig['attr']);
            $v['goods_input'] = implode(",",$goodsConfig['input']);
            $list[$k] = $v;
        }
        return $list;
    }

    /**
     * 获取商品信息
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo($id){
        $info = $this->where('id',$id)->find()->toArray();
        $goodsConfig = json_decode($info['goods_config'],true);
        $info['goods_config'] = $goodsConfig;
        $info['goods_attr'] =  implode(",",$goodsConfig['attr']);;
        $info['goods_input'] = implode(",",$goodsConfig['input']);
        return $info;
    }

    /**
     * 添加
     * @param $data
     */
    public function add($data){
        self::create($data);
    }

    /**
     * 编辑
     * @param $data
     */
    public function edit($data){
        self::update($data);
    }

    /**
     * 删除
     * @param $id
     */
    public function del($id){
        $this->where('id',$id)->delete();
    }

}
